package com.yjt.admin.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjt.admin.system.constant.SysCons;
import com.yjt.admin.system.dao.EmplyMapper;
import com.yjt.admin.system.dao.OrgMapper;
import com.yjt.admin.system.entity.EmpPost;
import com.yjt.admin.system.entity.Emply;
import com.yjt.admin.system.form.EmplyForm;
import com.yjt.admin.system.param.EmplyParam;
import com.yjt.admin.system.param.org.OrgParam;
import com.yjt.admin.system.service.EmpPostService;
import com.yjt.admin.system.service.EmplyService;
import com.yjt.admin.system.vo.EmplyVO;
import com.yjt.admin.system.vo.org.OrgVO;
import com.yjt.common.utils.ComUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author zzp
 * @since 2021-10-11
 */
@Service
public class EmplyServiceImpl extends ServiceImpl<EmplyMapper, Emply> implements EmplyService {

    @Resource
    private EmplyMapper emplyMapper;

    @Autowired
    private EmpPostService empPostService;

    @Resource
    private OrgMapper orgMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addEmply(EmplyForm emplyForm) {
        //保存人员信息
        Emply emply = Emply.builder().build();
        BeanUtils.copyProperties(emplyForm, emply);

        //需要为每个员工创建登录密码
        String bcyPassword = BCrypt.hashpw(SysCons.DEFAULT_PASSWORD, BCrypt.gensalt());
        emply.setLogonName(emply.getMobileNum());
        emply.setPwd(bcyPassword);
        this.save(emply);

        //新增岗位到员工岗位关联表
        EmpPost empPost = EmpPost.builder().emplyId(emply.getEmplyId()).postId(emply.getPostId()).build();
        empPostService.save(empPost);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEmply(EmplyForm emplyForm) {
        //查询原来岗位信息
        Emply emplyPo = this.getById(emplyForm.getEmplyId());

        //查询员工岗位关联信息
        EmpPost empPostPo = empPostService.getOne(Wrappers.<EmpPost>lambdaQuery().eq(EmpPost::getEmplyId, emplyPo.getEmplyId()).eq(EmpPost::getPostId, emplyPo.getPostId()));

        //修改岗位到员工岗位关联表
        empPostPo.setPostId(emplyForm.getPostId());
        empPostService.updateById(empPostPo);

        //修改人员信息
        Emply emply = Emply.builder().build();
        BeanUtils.copyProperties(emplyForm, emply);
        this.updateById(emply);
    }

    @Override
    public EmplyVO getOneEmply(String emplyId) {
        return emplyMapper.findOneEmply(emplyId);
    }

    @Override
    public IPage<EmplyVO> getPageEmply(EmplyParam emplyParam) {
        IPage<EmplyVO> emplyVOIPage = emplyMapper.findPageEmply(emplyParam.toPage(), emplyParam);
        List<EmplyVO> emplyVOList = emplyVOIPage.getRecords();
        if (!ComUtil.isNotNullList(emplyVOList)) {
            return emplyVOIPage;
        }

        //查询所有组织信息
        OrgParam orgParam = new OrgParam();
        orgParam.setIsForbid(1);
        List<OrgVO> orgVOList = orgMapper.findAllOrg(orgParam);
        emplyVOList.stream().forEach(emplyVO -> {
            //找出上级组织信息
            String superOrgId = emplyVO.getSuperOrgId();
            List<String> superNameList = this.getAllSuperName(orgVOList, superOrgId, new ArrayList<>());
            emplyVO.setSuperOrgName(this.setOrgSuperName(superNameList, ""));

            //找出实际岗位上级组织信息
            String postSuperOrgId = emplyVO.getPostSuperOrgId();
            List<String> postSuperNameList = this.getAllSuperName(orgVOList, postSuperOrgId, new ArrayList<>());
            emplyVO.setPostName(this.setOrgSuperName(postSuperNameList, emplyVO.getPostName()));

            //找出虚拟岗位上级组织信息
            String imagePostSuperOrgId = emplyVO.getImagePostSuperOrgId();
            List<String> imagePostSuperNameList = this.getAllSuperName(orgVOList, imagePostSuperOrgId, new ArrayList<>());
            emplyVO.setImagePostName(this.setOrgSuperName(imagePostSuperNameList, emplyVO.getImagePostName()));
        });

        emplyVOIPage.setRecords(emplyVOList);
        return emplyVOIPage;
    }

    /**
     * 设置上级组织名称
     *
     * @param superNameList
     * @param name
     * @return
     */
    private String setOrgSuperName(List<String> superNameList, String name) {
        StringBuilder superNames = new StringBuilder();
        for (int i = superNameList.size() - 1; i >= 0; i--) {
            superNames.append(superNameList.get(i));
            if (i > 0) {
                superNames.append("-");
            }
        }
        if (StringUtils.isNotBlank(name)) {
            superNames.append("-");
            superNames.append(name);
        }
        return superNames.toString();
    }


    /**
     * 获取所有的上级组织名称
     *
     * @param orgVOList
     * @param superOrgId
     * @param superNameList
     * @return
     */
    private List<String> getAllSuperName(List<OrgVO> orgVOList, String superOrgId, List<String> superNameList) {
        if (!ComUtil.isNotNullList(orgVOList) || !ComUtil.isNotNullStr(superOrgId)) {
            return superNameList;
        }
        for (OrgVO orgVO : orgVOList) {
            if (orgVO.getOrgId().equals(superOrgId)) {
                superNameList.add(orgVO.getOrgName());
                this.getAllSuperName(orgVOList, orgVO.getSuperOrgId(), superNameList);
            }
        }
        return superNameList;
    }

}
